#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using UTable.Input.MultiTouch;
using System.Windows;


namespace UTable.Input
{
	public class SystemMouseProvider: IInputProvider
	{
        public void Initialize(UTable.Objects.UApplication context)
        {
            this.CurrentWindow = context.MainWindow;
            CurrentWindow.MouseLeftButtonDown += new MouseButtonEventHandler(SystemMouseProvider_MouseLeftButtonDown);
            CurrentWindow.MouseMove += new MouseEventHandler(SystemMouseProvider_MouseMove);
            CurrentWindow.MouseLeftButtonUp += new MouseButtonEventHandler(SystemMouseProvider_MouseLeftButtonUp);
        }


		private void SystemMouseProvider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			isLeftMouseDown = true;
			Collection<FingerEventArgs> fingerevents = new Collection<FingerEventArgs>();
			FingerEventArgs fingerevent = new FingerEventArgs();
			fingerevent.EventType = FingerEventType.FINGER_DOWN_EVENT;
			fingerevent.FingerID = currentFingerID;
			fingerevent.Position = e.GetPosition(CurrentWindow);
            fingerevent.GlobalPosition = fingerevent.Position;
			fingerevents.Add(fingerevent);
			MultiTouchEventArgs args = new MultiTouchEventArgs(fingerevents, InputEventArgs.GetTimestamp());
            if (InputTriggered != null)
			    InputTriggered(args);
		}

		private void SystemMouseProvider_MouseMove(object sender, MouseEventArgs e)
		{
			if (isLeftMouseDown)
			{
				Collection<FingerEventArgs> fingerevents = new Collection<FingerEventArgs>();
				FingerEventArgs fingerevent = new FingerEventArgs();
				fingerevent.EventType = FingerEventType.FINGER_MOVE_EVENT;
				fingerevent.FingerID = currentFingerID;
                fingerevent.Position = e.GetPosition(CurrentWindow);
                fingerevent.GlobalPosition = fingerevent.Position;
				fingerevents.Add(fingerevent);
				MultiTouchEventArgs args = new MultiTouchEventArgs(fingerevents, InputEventArgs.GetTimestamp());
				if (InputTriggered != null)
                    InputTriggered(args);
			}
		}

		private void SystemMouseProvider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (isLeftMouseDown)
			{
				Collection<FingerEventArgs> fingerevents = new Collection<FingerEventArgs>();
				FingerEventArgs fingerevent = new FingerEventArgs();
				fingerevent.EventType = FingerEventType.FINGER_UP_EVENT;
				fingerevent.FingerID = currentFingerID;
                fingerevent.Position = e.GetPosition(CurrentWindow);
                fingerevent.GlobalPosition = fingerevent.Position;
				fingerevents.Add(fingerevent);
				MultiTouchEventArgs args = new MultiTouchEventArgs(fingerevents, InputEventArgs.GetTimestamp());
                if (InputTriggered != null)
				    InputTriggered(args);
				isLeftMouseDown = false;
				currentFingerID++;
			}
		}

		private int currentFingerID = 0;

		private bool isLeftMouseDown = false;

        private Window CurrentWindow;

        public event InputEventHandler InputTriggered;

	}
}
